package cc.magicjson.config;

import cc.magicjson.task.MyScheduledTask;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

@Configuration
@EnableScheduling
@ConditionalOnProperty(name = "scheduling.enabled", havingValue = "true", matchIfMissing = true)
public class ScheduledTaskConfig implements SchedulingConfigurer {

	private final MyScheduledTask myScheduledTask;

	public ScheduledTaskConfig(MyScheduledTask myScheduledTask) {
		this.myScheduledTask = myScheduledTask;
	}

	@Override
	public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
		// 注册定时任务
		taskRegistrar.addFixedRateTask(new Runnable() {
			@Override
			public void run() {
				myScheduledTask.performTask();
			}
		}, 6000);  // 每6秒执行一次
	}
}
